
import mysql = require("mysql")
import { Logger } from "./Logger";


export class MySqlUtil {

    private static MySql_Host: string = "127.0.0.1"; //
    private static MySql_Port: number = 3306; //
    private static MySql_User: string = "root"
    private static MySql_Pass: string = "666666";
    private static Mysql_database: string = "haogame";

    private static connection: any;

    private static isInited:boolean = false;

    public static init() {
        if(this.isInited){
            return;
        }
        this.isInited = true;
        let config: object = {
            "host": this.MySql_Host,
            "user": this.MySql_User,
            "password": this.MySql_Pass,
            "database": this.Mysql_database
        };
        this.connection = mysql.createConnection(config);
        this.connection.connect();
        // let test:string = "SELECT * FROM users;";
        // let test:string = "INSERT INTO users values(null, 100003, 'test3', 'nickname3');";
        // this.executeSql(test);
    }

    public static executeSql(sql: string, callback:Function) {
        this.init();
        this.connection.query(sql, (err, result) => {
            if (err) {
                Logger.warn("executeSql error ", err.message);
                callback(false, err);
                return;
            }
            // Logger.log("executeSql res=", result);
            callback(true, result);
        });
        // this.connection.end();
    }

}